#define _CRT_SECURE_NO_WARNINGS 1

class NeighborSum {
public:
    NeighborSum(vector<vector<int>>& grid) {
        vv = grid;
    }

    int adjacentSum(int value) {
        int sum = 0;
        vector<int> v2 = Find(value);
        if (v2[0] != 0)
            sum += vv[v2[0] - 1][v2[1]];

        if (v2[0] != vv.size() - 1)
            sum += vv[v2[0] + 1][v2[1]];

        if (v2[1] != 0)
            sum += vv[v2[0]][v2[1] - 1];

        if (v2[1] != vv.size() - 1)
            sum += vv[v2[0]][v2[1] + 1];

        return sum;
    }

    int diagonalSum(int value) {
        int sum = 0;
        vector<int> v2 = Find(value);
        if (v2[0] != 0 && v2[1] != 0)
            sum += vv[v2[0] - 1][v2[1] - 1];

        if (v2[0] != vv.size() - 1 && v2[1] != 0)
            sum += vv[v2[0] + 1][v2[1] - 1];

        if (v2[0] != 0 && v2[1] != vv.size() - 1)
            sum += vv[v2[0] - 1][v2[1] + 1];

        if (v2[0] != vv.size() - 1 && v2[1] != vv.size() - 1)
            sum += vv[v2[0] + 1][v2[1] + 1];

        return sum;
    }

    vector<int> Find(int value)
    {
        vector<int> v1;
        for (int i = 0; i < vv.size(); ++i)
        {
            for (int j = 0; j < vv.size(); ++j)
            {
                if (vv[i][j] == value)
                {
                    v1.push_back(i);
                    v1.push_back(j);
                    return v1;
                }
            }
        }

        return v1;
    }

    vector<vector<int>> vv;
};

/**
 * Your NeighborSum object will be instantiated and called as such:
 * NeighborSum* obj = new NeighborSum(grid);
 * int param_1 = obj->adjacentSum(value);
 * int param_2 = obj->diagonalSum(value);
 */